package com.har.modules.web.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.har.common.exception.RRException;
import com.har.common.utils.DateUtils;
import com.har.common.utils.R;
import com.har.common.utils.StringUtil;
import com.har.modules.web.dao.RegiserDao;
import com.har.modules.web.entity.ApplyEntity;
import com.har.modules.web.entity.RegiserEntity;
import com.har.modules.web.service.RegiserService;
import com.har.modules.web.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Service("regiserService")
public class RegiserServiceImpl extends ServiceImpl<RegiserDao, RegiserEntity> implements RegiserService {

    @Autowired
    private SmsService smsService;

    @Override
    public R save(ApplyEntity applyEntity, HttpServletRequest request) throws RRException, ParseException {
        log.info("mobile>>>{}>>>verificationcode>>>{}",applyEntity.getMobile(),applyEntity.getVerificationCode());
        //申请之前验证用户是否登录
//        Cookie[] cookies=request.getCookies();
//        if(null!=cookies){
//            String str=null;
//            for (Cookie cookie:cookies){
//                if(cookie.getName().equals("login")){
//                    str=cookie.getValue();
//                    log.info("cookie>>>{}",cookie.getName());
//                }
//            }
//            if(str==null){
//                throw new RRException("请先注册或登录账号");
//            }
//        }else {
//            throw new RRException("请先注册或登录账号");
//        }
        //通过验证码和手机号进行验证
       /* SmsEntity sms=smsService.selectOne(new EntityWrapper<SmsEntity>()
                .eq("mobile",applyEntity.getMobile())
                .eq("verification_code",applyEntity.getVerificationCode()));
        if(null==sms){
            throw new RRException("验证码错误，请输入正确的验证码");
        }*/
        Date day=new Date();
        SimpleDateFormat df = new SimpleDateFormat(DateUtils.DATE_TIME_PATTERN);
        //创建时间
        applyEntity.setCreateTime(DateUtils.stringToDate(df.format(day),DateUtils.DATE_TIME_PATTERN));
      /*  DateFormat dateFormat = new SimpleDateFormat(DateUtils.DATE_TIME_PATTERN);
        //当前时间
        Date  dateTime1 = dateFormat.parse(df.format(applyEntity.getCreateTime()));
        //失效时间
        Date  dateTime2 = dateFormat.parse(df.format(sms.getExpireTime()));
        //比较时间
        if(dateTime1.after(dateTime2)){
            throw new RRException("验证码已失效，请重新获取",400);
        }else {*/
            //保存申请
            RegiserEntity regiserEntity=new RegiserEntity();
            if(StringUtil.strLength(applyEntity.getCompanyName())>20){
                throw new RRException("请输入20位以内的企业名称。");
            }
            if(StringUtil.strLength(applyEntity.getUserName())>20){
                throw new RRException("请输入20位以内的姓名");
            }

            if(StringUtil.strLength(applyEntity.getLoanAmount())>20){
                throw new RRException("请输入20位以内的贷款额度。");
            }
            /**
             * 贷款期限前端分两个参数传 后台只有一个参数保存 进行拼接
             */
            log.info("year>>>{}------month>>>{}",applyEntity.getYear(),applyEntity.getMonth());
            if(!StringUtil.isEmpty(applyEntity.getMonth())&&!StringUtil.isEmpty(applyEntity.getYear())){//两个参数都不为空时直接拼接
                regiserEntity.setLoanTimeLimit(applyEntity.getYear()+"年"+applyEntity.getMonth()+"月");
            }
            if(StringUtil.isEmpty(applyEntity.getYear())){//当年为空 月不为空时
                if(!StringUtil.isEmpty(applyEntity.getMonth())){
                    regiserEntity.setLoanTimeLimit(applyEntity.getMonth()+"月");
                }
            }
            if (StringUtil.isEmpty(applyEntity.getMonth())){//当月为空 年不为空时
                if(!StringUtil.isEmpty(applyEntity.getYear())){
                    regiserEntity.setLoanTimeLimit(applyEntity.getYear()+"年");
                }
            }
            if(StringUtil.strLength(regiserEntity.getLoanTimeLimit())>20){
                throw new RRException("请输入20位以内的贷款期限。");
            }
            if(StringUtil.strLength(applyEntity.getAdvantage())>255){
                throw new RRException("请输入255位以内的需求。");
            }
            regiserEntity.setProductTypeName(applyEntity.getProductTypeName());
            regiserEntity.setSource(applyEntity.getSource());
            regiserEntity.setMobile(applyEntity.getMobile());
            regiserEntity.setUserName(applyEntity.getUserName());
            regiserEntity.setProductName(applyEntity.getProductName());
            regiserEntity.setCompanyName(applyEntity.getCompanyName());
            regiserEntity.setAdvantage(applyEntity.getAdvantage());
            regiserEntity.setLoanAmount(applyEntity.getLoanAmount());
            regiserEntity.setCreateTime(applyEntity.getCreateTime());
            regiserEntity.setDemandDescribe(applyEntity.getDemandDescribe());
            this.insert(regiserEntity);
//        }
        return R.ok();
    }


}
